package com.xinshujia.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.xinshujia.entity.ThresholdEntity;
import com.xinshujia.response.RespResult;
import com.xinshujia.service.ThresholdService;
import com.xinshujia.utils.GsonUtil;
import org.springframework.web.bind.annotation.*;

/**
 * *************************************
 * *   @author: 野狼上校               **
 * *   @since: 2024-08-06 18:16       **
 * *   @version: 1.0                  **
 * *************************************
 * <p>
 */
@RestController
@RequestMapping("threshold")
public class ThresholdController {
    private final ThresholdService thresholdService;

    public ThresholdController(ThresholdService thresholdService) {
        this.thresholdService = thresholdService;
    }

    /**
     * 设置或更新阈值
     */
    @PostMapping("setOrUpdateThreshold")
    public RespResult setOrUpdateThreshold(@RequestBody ThresholdEntity thresholdEntity) {
        Boolean flag = thresholdService.setOrUpdateThreshold(thresholdEntity);
        return flag ? RespResult.OK() : RespResult.Fail();
    }

    /**
     * 分页查询所有设备的当前阈值
     */
    @GetMapping("findThresholdsByPage")
    public RespResult findThresholdsByPage(@RequestParam("pageNum") Long pageNum,
                                           @RequestParam("pageSize") Long pageSize) {
        Page<ThresholdEntity> pages = thresholdService.findThresholdsByPage(pageNum, pageSize);
        Gson gson = GsonUtil.googleGson();
        return RespResult.OK(gson.toJson(pages));
    }

    /**
     * 根据deviceGuid查询该设备所有的阈值记录
     */
    @GetMapping("findThresholdsByDeviceGuid")
    public RespResult findThresholdsByDeviceGuid(@RequestParam("pageSize") Long pageSize,
                                                 @RequestParam("pageNum") Long pageNum,
                                                 @RequestParam("deviceGuid") String deviceGuid) {
        Page<ThresholdEntity> pages = thresholdService.findThresholdsByDeviceGuid(pageNum, pageSize, deviceGuid);
        Gson gson = GsonUtil.googleGson();
        return RespResult.OK(gson.toJson(pages));
    }

}
